{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(2018)\n",
    "x = 12 * np.random.rand(50)\n",
    "y = 3.14 * x - 1.414 + np.random.randn(50)\n",
    "plt.scatter(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 选择一个模型的类\n",
    "from sklearn.linear_model import LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 选择模型的超参数，实例化一个模型\n",
    "# 超参数必须在模型对数据做拟合(fit)之前设定\n",
    "model = LinearRegression(fit_intercept=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预备特征矩阵和目标阵列（这里的目标阵列y已经就绪，无需做额外的工作）\n",
    "X = x[:,None]\n",
    "X.shape, y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练模型\n",
    "model.fit(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fit()方法将会触发模型内部的计算，计算的结果存储在模型的属性中，用户可以查看这些属性。\n",
    "# 按照约定，fit()方法运行过程中学到的模型参数，名字以下划线结尾\n",
    "\n",
    "# 这里的系数和截距非常接近3.14和-1.414, (y = 3.14 * x - 1.414 + np.random.randn(50))\n",
    "model.coef_, model.intercept_\n",
    "\n",
    "# Scikit-Learn并没有提供工具用来解释模型的内部参数，这个问题更多的是统计建模的问题，而不是机器学习问题，\n",
    "# 机器学习聚焦于模型的预测结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预备测试数据\n",
    "xtest = np.linspace(-1, 13)\n",
    "Xfit = xtest[:,None]\n",
    "yfit = model.predict(Xfit)\n",
    "yfit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输出原始数据和预测结果\n",
    "plt.scatter(x, y)\n",
    "plt.plot(Xfit, yfit)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
